Migrated the PushbulletAgent to use LiquidInterpolatable

Dominik Sander 11 years ago
parent
commit
addb4dbdbf

+ 8 - 15
app/models/agents/pushbullet_agent.rb

@@ -1,6 +1,6 @@
1 1
 module Agents
2 2
   class PushbulletAgent < Agent
3
-    include JsonPathOptionsOverwritable
3
+    include LiquidInterpolatable
4 4
 
5 5
     cannot_be_scheduled!
6 6
     cannot_create_events!
@@ -20,7 +20,7 @@ module Agents
20 20
 
21 21
       You can provide a `title` and a `body`.
22 22
 
23
-      If you want to specify `title` or `body` per event, you can provide a [JSONPath](http://goessner.net/articles/JsonPath/) for each of them.
23
+      In every value of the options hash you can use the [Liquid templating syntax](http://liquidmarkup.org/).
24 24
     MD
25 25
 
26 26
     def default_options
@@ -28,9 +28,7 @@ module Agents
28 28
         'api_key' => '',
29 29
         'device_id' => '',
30 30
         'title' => "Hello from Huginn!",
31
-        'title_path' => '',
32
-        'body' => '',
33
-        'body_path' => '',
31
+        'body' => '{{body}}',
34 32
       }
35 33
     end
36 34
 
@@ -52,16 +50,11 @@ module Agents
52 50
 
53 51
     private
54 52
     def query_options(event)
55
-      mo = merge_json_path_options event
56
-      basic_options.deep_merge(:body => {:title => mo[:title], :body => mo[:body]})
57
-    end
58
-
59
-    def basic_options
60
-      {:basic_auth => {:username =>options[:api_key], :password=>''}, :body => {:device_iden => options[:device_id], :type => 'note'}}
61
-    end
62
-
63
-    def options_with_path
64
-      [:title, :body]
53
+      mo = interpolate_options options, event.payload
54
+      {
55
+        :basic_auth => {:username =>mo[:api_key], :password=>''},
56
+        :body => {:device_iden => mo[:device_id], :title => mo[:title], :body => mo[:body], :type => 'note'}
57
+      }
65 58
     end
66 59
   end
67 60
 end

+ 7 - 0
db/migrate/20140430215234_migrate_pushbullet_agent_to_liquid.rb

@@ -0,0 +1,7 @@
1
+class MigratePushbulletAgentToLiquid < ActiveRecord::Migration
2
+  def change
3
+    Agent.where(:type => 'Agents::PushbulletAgent').each do |agent|
4
+      LiquidMigrator.convert_all_agent_options(agent)
5
+    end
6
+  end
7
+end

+ 9 - 14
spec/models/agents/pushbullet_agent_spec.rb

@@ -1,14 +1,14 @@
1 1
 require 'spec_helper'
2
-require 'models/concerns/json_path_options_overwritable'
2
+require 'models/concerns/liquid_interpolatable'
3 3
 
4 4
 describe Agents::PushbulletAgent do
5
-  it_behaves_like JsonPathOptionsOverwritable
5
+  it_behaves_like LiquidInterpolatable
6 6
 
7 7
   before(:each) do
8 8
     @valid_params = {
9 9
                       'api_key' => 'token',
10 10
                       'device_id' => '124',
11
-                      'body_path' => '$.body',
11
+                      'body' => '{{body}}',
12 12
                       'title' => 'hello from huginn'
13 13
                     }
14 14
 
@@ -39,23 +39,18 @@ describe Agents::PushbulletAgent do
39 39
   end
40 40
 
41 41
   describe "helpers" do
42
-    it "it should return the correct basic_options" do
43
-      @checker.send(:basic_options).should == {:basic_auth => {:username =>@checker.options[:api_key], :password=>''},
44
-                                               :body => {:device_iden => @checker.options[:device_id], :type => 'note'}}
45
-    end
46
-
47
-
48 42
     it "should return the query_options" do
49
-      @checker.send(:query_options, @event).should == @checker.send(:basic_options).deep_merge({
50
-        :body => {:title => 'hello from huginn', :body => 'One two test'}
51
-      })
43
+      @checker.send(:query_options, @event).should == {
44
+        :body => {:title => 'hello from huginn', :body => 'One two test', :device_iden => @checker.options[:device_id], :type => 'note'},
45
+        :basic_auth => {:username =>@checker.options[:api_key], :password=>''}
46
+      }
52 47
     end
53 48
   end
54 49
 
55 50
   describe "#receive" do
56 51
     it "send a message to the hipchat" do
57 52
       stub_request(:post, "https://token:@api.pushbullet.com/api/pushes").
58
-        with(:body => "device_iden=124&type=note&title=hello%20from%20huginn&body=One%20two%20test").
53
+        with(:body => "device_iden=124&title=hello%20from%20huginn&body=One%20two%20test&type=note").
59 54
         to_return(:status => 200, :body => "ok", :headers => {})
60 55
       dont_allow(@checker).error
61 56
       @checker.receive([@event])
@@ -63,7 +58,7 @@ describe Agents::PushbulletAgent do
63 58
 
64 59
     it "should log resquests which return an error" do
65 60
       stub_request(:post, "https://token:@api.pushbullet.com/api/pushes").
66
-        with(:body => "device_iden=124&type=note&title=hello%20from%20huginn&body=One%20two%20test").
61
+        with(:body => "device_iden=124&title=hello%20from%20huginn&body=One%20two%20test&type=note").
67 62
         to_return(:status => 200, :body => "error", :headers => {})
68 63
       mock(@checker).error("error")
69 64
       @checker.receive([@event])